//
//  FDMTopRadioCollectionViewCell.m
//  Entertainment
//
//  Created by dllo on 16/4/22.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "FDMTopRadioCollectionViewCell.h"
#import "FDMTopRadioListCollectionViewCell.h"
#import "FDMTopRadioDataModel.h"
#import "FDMTopRadioDetailModel.h"
#import "FDMHotRecommendsHeaderView.h"
#import "FDMTopRadioWheelModel.h"
#import "FDMTopCollectionReusableViewHeader.h"

@interface FDMTopRadioCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) FDMBaseCollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataModelArr;
@property (nonatomic, retain) NSMutableArray *focusImagesModelArr;

@end
@implementation FDMTopRadioCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollectionView];
        [self dataHandel];
    }
    return self;
}

- (void)dealloc {
    [self.collectionView release];
    [self.dataModelArr release];
    [self.focusImagesModelArr release];
    [super dealloc];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH - 20, 100);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[FDMBaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 153) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:1.00 green:0.81 blue:0.79 alpha:1.00];
    [self.contentView addSubview:self.collectionView];
    // 节目榜单
    [self.collectionView registerClass:[FDMTopRadioListCollectionViewCell class] forCellWithReuseIdentifier:@"FDMTopRadioListCollectionViewCell"];
    // 头视图
    [self.collectionView registerClass:[FDMHotRecommendsHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FDMTopHeaderView"];
    // 轮播图
    [self.collectionView registerClass:[FDMTopCollectionReusableViewHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FDMTopCollectionReusableViewHeader"];
}

#pragma mark - collectionViewDelegate
// 分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section > 0) {
        return CGSizeMake(SCREEN_WIDTH, 30);
    }
    return CGSizeMake(SCREEN_WIDTH, 150);
}

// cell 个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section > 0 && section < 3) {
        FDMTopRadioDataModel *model = self.dataModelArr[section - 1];
        return model.listModelArray.count;
    }
    return 0;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section > 0) {
        if (kind == UICollectionElementKindSectionHeader) {
            FDMHotRecommendsHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FDMTopHeaderView" forIndexPath:indexPath];
            FDMTopRadioDataModel *model = self.dataModelArr[indexPath.section - 1];
            header.titleLabel.text = model.title;
            return header;
        }
    }
    if (indexPath.section == 0) {
        FDMTopCollectionReusableViewHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FDMTopCollectionReusableViewHeader" forIndexPath:indexPath];
        FDMTopRadioWheelModel *model = self.focusImagesModelArr[indexPath.section];
        header.headerImageModel = model;
        return header;
    }
        return nil;
    
}

// cell赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section > 0) {
        FDMTopRadioListCollectionViewCell  *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FDMTopRadioListCollectionViewCell" forIndexPath:indexPath];
        FDMTopRadioDataModel *model = self.dataModelArr[indexPath.section - 1];
        FDMTopRadioDetailModel *detailModel = model.listModelArray[indexPath.row];
        cell.detailModel = detailModel;
        
        return cell;
        
    }
    return nil;
}

- (void)dataHandel {
    [APPTools GETWithURL:@"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=and-a1&device=android&includeActivity=true&includeSpecial=true&scale=2&statEvent=pageview/tab@发现_榜单&version=4.3.80" par:nil success:^(id responseObject) {
        self.dataModelArr = [FDMTopRadioDataModel baseModelHandelWithArray:responseObject[@"datas"]];
        self.focusImagesModelArr = [FDMTopRadioWheelModel baseModelHandelWithArray:responseObject[@"focusImages"][@"list"]];
        [self.collectionView reloadData];
    } failed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
